<?php

namespace app\index\service;


use app\index\model\ContractCaiModel;
use app\index\model\ContractShiModel;
use app\index\model\DeptModel;
use app\index\model\EnginModel;
use app\index\model\OptionFeiModel;
use app\index\model\ZhiJieModel;

class FactorService
{
    public function engin($factor, $minfo){
        $minfo['enginid_name'] = $minfo['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($minfo['chanid']);
        $minfo['business_name'] = $minfo['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function shou_tou($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['feilei_name'] = OptionFeiModel::where(['id'=>$minfo['feilei']])->value('name');
        return $this->check_valid($factor, $minfo);
    }
    public function zhi_tou($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['feilei_name'] = OptionFeiModel::where(['id'=>$minfo['feilei']])->value('name');
        return $this->check_valid($factor, $minfo);
    }




    public function contract_shi($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['shihetype_name'] = (new OptService())->get_base_showname('engin_shihetype', $minfo['shihetype']);
        return $this->check_valid($factor, $minfo);
    }
    public function contract_bu($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $shiinfo = ContractShiModel::get_info_by_pk($minfo['shiid']);
        $minfo['shihetype_name'] = (new OptService())->get_base_showname('engin_shihetype', $shiinfo['shihetype']);
        return $this->check_valid($factor, $minfo);
    }
    public function contract_shi_jie($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function engin_visa($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['visatype_name'] = (new OptService())->get_base_showname('scene_visatype', $minfo['visatype']);
        return $this->check_valid($factor, $minfo);
    }
    public function engin_chan($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function piao_kai($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['piaotype_name'] = (new PiaoService())->get_kai_piaotype_name($minfo['piaotype']);
        return $this->check_valid($factor, $minfo);
    }
    public function shou_xiang($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['feilei_name'] = OptionFeiModel::where(['id'=>$minfo['feilei']])->value('name');
    }
    public function engin_budget($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function engin_budbian($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function contract_cai($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['caitype_name'] = (new ContractService())->get_contract_cai_caitype_name($minfo['caitype']);
        return $this->check_valid($factor, $minfo);
    }
    public function contract_bian($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $caiinfo = ContractCaiModel::get_info_by_pk($minfo['caiid']);
        $minfo['caitype_name'] = (new ContractService())->get_contract_cai_caitype_name($caiinfo['caitype']);
        return $this->check_valid($factor, $minfo);
    }
    public function contract_cai_jie($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $caiinfo = ContractCaiModel::get_info_by_pk($minfo['caiid']);
        $minfo['caitype_name'] = (new ContractService())->get_contract_cai_caitype_name($caiinfo['caitype']);
        return $this->check_valid($factor, $minfo);
    }
    public function contract_buy($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['buytype_name'] = (new ContractService())->get_contract_buy_buytype_name($minfo['buytype']);
        return $this->check_valid($factor, $minfo);
    }
    public function piao_shou($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function zhi_xiang($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['feilei_name'] = OptionFeiModel::where(['id'=>$minfo['feilei']])->value('name');
        return $this->check_valid($factor, $minfo);
    }
    public function zhi_enbao($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function contract_lian($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['myer_name'] = DeptModel::get_name_by_pk($minfo['myer']);
        return $this->check_valid($factor, $minfo);
    }
    public function dai_kou_qi($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function zhi_jintui($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function zhi_lianjie($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function stuff_ru($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['rutype_name'] = (new StuffService())->get_stuff_ru_rutype_name($minfo['rutype']);
        return $this->check_valid($factor, $minfo);
    }
    public function device_jin($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function device_yun($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function device_tui($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function labour_gong($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function labour_yong($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function labour_xiang($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }


    public function engin_diary($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function engin_speed($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function zhi_daybao($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function zhi_dayfu($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function shou_qi($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function contract_day($factor, $minfo){
        $minfo['helei_name'] = (new OptService())->get_base_showname('cai_contract_day_helei', $minfo['helei']);
        $minfo['caier_name'] = DeptModel::get_name_by_pk($minfo['caier']);
        return $this->check_valid($factor, $minfo);
    }
    public function piao_tax($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function zhi_jie($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['feitype_name'] = OptionFeiModel::where(['id'=>$minfo['feitype']])->value('name');
        return $this->check_valid($factor, $minfo);
    }
    public function shou_huan($factor, $minfo){
        $jieinfo = ZhiJieModel::get_info_by_pk($minfo['jieid']);
        $engin = EnginModel::get_info_by_pk($jieinfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['feitype_name'] = OptionFeiModel::where(['id'=>$jieinfo['feitype']])->value('name');
        return $this->check_valid($factor, $minfo);
    }
    public function bond_shou($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['feitype_name'] = OptionFeiModel::where(['id'=>$minfo['feitype']])->value('name');
        return $this->check_valid($factor, $minfo);
    }
    public function bond_jiao($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['feitype_name'] = OptionFeiModel::where(['id'=>$minfo['feitype']])->value('name');
        return $this->check_valid($factor, $minfo);
    }
    public function bond_tui($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['feitype_name'] = OptionFeiModel::where(['id'=>$minfo['feitype']])->value('name');
        return $this->check_valid($factor, $minfo);
    }
    public function bond_fan($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['feitype_name'] = OptionFeiModel::where(['id'=>$minfo['feitype']])->value('name');
        return $this->check_valid($factor, $minfo);
    }
    public function bond_kai($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['hantype_name'] = (new OptService())->get_base_showname('engin_bond_hantype', $minfo['hantype']);
        return $this->check_valid($factor, $minfo);
    }
    public function dealcom($factor, $minfo){
        $minfo['type_name'] = (new OptService())->get_base_showname('cai_dealcom_type', $minfo['type']);
        return $this->check_valid($factor, $minfo);
    }
    public function dealbian($factor, $minfo){
        $minfo['type_name'] = (new OptService())->get_base_showname('cai_dealcom_type', $minfo['type']);
        return $this->check_valid($factor, $minfo);
    }

    public function wage($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function cert_use($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['yong_name'] = (new OptService())->get_base_showname('cert_use_yong', $minfo['yong']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function cert_down($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function cert_borrow($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function cert_employ($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function cert_empbu($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function seal_gai($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function seal_wai($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function seal_zhi($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        return $this->check_valid($factor, $minfo);
    }
    public function wu_cai($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function wu_ling($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function dang_jie($factor, $minfo){
        return $this->check_valid($factor, $minfo);
    }
    public function engin_stamp($factor, $minfo){
        $engin = EnginModel::get_info_by_pk($minfo['enginid']);
        $minfo['enginid_name'] = $engin['name'];
        $minfo['chanid_name'] = DeptModel::get_name_by_pk($engin['chanid']);
        $minfo['business_name'] = $engin['business'] == 1 ? '自营' : '联营';
        $minfo['zitype_name'] = (new OptService())->get_base_showname('scene_zitype', $minfo['zitype']);
        return $this->check_valid($factor, $minfo);
    }
    public function tongyong($factor, $minfo){
        $minfo['shilei_name'] = (new OptService())->get_base_showname('qi_tongyong_shilei', $minfo['shilei']);
        return $this->check_valid($factor, $minfo);
    }









    private function check_valid($factor, $minfo){
        $result = false;
        foreach ($factor as $fac){
            $fres = true;
            foreach ($fac as $item){
                if(!$this->check_field_factor($minfo[$item['fieldname']], $item['fieldtype'], $item['fieldvalue'])){
                    $fres = false; continue;
                }
            }
            if($fres){
                $result = true; break;
            }
        }
        return $result;
    }
    private function check_field_factor($value, $conder, $convalue){
        if($conder == 'like'){
            $convalueArr = explode(',', $convalue);
            $zongRes = false;
            foreach ($convalueArr as $ccc){
                if(empty($ccc)){
                    continue;
                }
                if(strpos($value, $ccc) !== false){
                    $zongRes = true;
                    continue;
                }
            }
            return $zongRes;
        }elseif($conder == 'notlike'){
            $convalueArr = explode(',', $convalue);
            $zongRes = false;
            foreach ($convalueArr as $ccc){
                if(empty($ccc)){
                    continue;
                }
                if(strpos($value, $ccc) === false){
                    $zongRes = true;
                    continue;
                }
            }
            return $zongRes;
        }elseif($conder == 'eq'){
            $convalueArr = explode(',', $convalue);
            $zongRes = false;
            foreach ($convalueArr as $ccc){
                if(empty($ccc)){
                    continue;
                }
                if($value == $ccc){
                    $zongRes = true;
                    continue;
                }
            }
            return $zongRes;
        }elseif($conder == 'neq'){
            $convalueArr = explode(',', $convalue);
            $zongRes = false;
            foreach ($convalueArr as $ccc){
                if(empty($ccc)){
                    continue;
                }
                if($value != $ccc){
                    $zongRes = true;
                    continue;
                }
            }
            return $zongRes;
        }elseif($conder == 'gt'){
            if($value > $convalue){
                return true;
            }
        }elseif($conder == 'egt'){
            if($value >= $convalue){
                return true;
            }
        }elseif($conder == 'lt'){
            if($value < $convalue){
                return true;
            }
        }elseif($conder == 'elt'){
            if($value <= $convalue){
                return true;
            }
        }

        return false;
    }

}
